package com.pinxixi.specification.mapper;

import com.pinxixi.framework.domain.specification.Specification;
import com.pinxixi.framework.domain.specification.SpecificationOption;
import com.pinxixi.framework.domain.specification.ext.SpecificationEntity;
import com.pinxixi.framework.domain.specification.ext.SpecificationExt;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.util.List;

@Mapper
public interface SpecificationMapper {

    @Select("select id,spec_name specName from tb_specification")
    List<Specification> findAll();

    @Select("select s.id,s.spec_name specName,GROUP_CONCAT(so.option_name) specOptions from tb_specification s LEFT JOIN tb_specification_option so on s.id = so.spec_id where s.spec_name like concat('%',#{specName},'%') GROUP BY s.id limit #{index},#{size}")
    List<SpecificationExt> findPage(Integer index, Integer size,String specName);

    @Select("select count(1) from tb_specification where spec_name like concat('%',#{specName},'%')")
    Integer selectCount(String specName);

    @Select("select id,spec_name specName from tb_specification where id=#{id}")
    Specification findOne(Integer id);

    @Select("select id,option_name optionName, spec_id specId,orders from tb_specification_option where spec_id=#{id}")
    List<SpecificationOption> findOptionsById(Integer id);

    void add(Specification specification);

    void addOption(SpecificationOption option);

    @Update("update tb_specification set spec_name=#{specName} where id=#{id}")
    void editSpecification(Specification specification);

    @Update("update tb_specification_option set option_name=#{optionName},orders=#{orders} where id=#{id}")
    void editOption(SpecificationOption option);

    void deleteOptions(SpecificationEntity entity);

    @Delete("delete from tb_specification where id=#{id}")
    void deleteSpecification(Integer id);

    @Delete("delete from tb_specification_option where spec_id=#{id}")
    void deleteOptionsBySpecId(Integer id);


}
